package com.ruoyi.experiment.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.experiment.domain.VirtualMachine;
import com.ruoyi.experiment.service.IVirtualMachineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 虚拟机模板Controller
 * 
 * @author 邓鑫
 * @date 2023-07-28
 */
@RestController
@RequestMapping("/experiment/machine")
public class VirtualMachineController extends BaseController
{
    @Autowired
    private IVirtualMachineService virtualMachineService;

    /**
     * 查询虚拟机模板列表
     */
    @PreAuthorize("@ss.hasPermi('experiment:machine:list')")
    @GetMapping("/list")
    public TableDataInfo list(VirtualMachine virtualMachine)
    {
        startPage();
        List<VirtualMachine> list = virtualMachineService.selectVirtualMachineList(virtualMachine);
        return getDataTable(list);
    }

    /**
     * 导出虚拟机模板列表
     */
    @PreAuthorize("@ss.hasPermi('experiment:machine:export')")
    @Log(title = "虚拟机模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VirtualMachine virtualMachine)
    {
        List<VirtualMachine> list = virtualMachineService.selectVirtualMachineList(virtualMachine);
        ExcelUtil<VirtualMachine> util = new ExcelUtil<VirtualMachine>(VirtualMachine.class);
        util.exportExcel(response, list, "虚拟机模板数据");
    }

    /**
     * 获取虚拟机模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:machine:query')")
    @GetMapping(value = "/{virtualMachineId}")
    public AjaxResult getInfo(@PathVariable("virtualMachineId") Long virtualMachineId)
    {
        return success(virtualMachineService.selectVirtualMachineByVirtualMachineId(virtualMachineId));
    }

    /**
     * 新增虚拟机模板
     */
    @PreAuthorize("@ss.hasPermi('experiment:machine:add')")
    @Log(title = "虚拟机模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VirtualMachine virtualMachine, @AuthenticationPrincipal UserDetails userDetails)
    {
        //设置添加账号
        String username = userDetails.getUsername();
        virtualMachine.setVirtualMachineAccount(username);
        return toAjax(virtualMachineService.insertVirtualMachine(virtualMachine));
    }

    /**
     * 修改虚拟机模板
     */
    @PreAuthorize("@ss.hasPermi('experiment:machine:edit')")
    @Log(title = "虚拟机模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VirtualMachine virtualMachine)
    {
        return toAjax(virtualMachineService.updateVirtualMachine(virtualMachine));
    }

    /**
     * 删除虚拟机模板
     */
    @PreAuthorize("@ss.hasPermi('experiment:machine:remove')")
    @Log(title = "虚拟机模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{virtualMachineIds}")
    public AjaxResult remove(@PathVariable Long[] virtualMachineIds)
    {
        return toAjax(virtualMachineService.deleteVirtualMachineByVirtualMachineIds(virtualMachineIds));
    }
}
